/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TCashCard;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JComboBoxPOJO;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TCashCardService;
import com.ruipengkj.view.base.BaseDialog;
/**
 * 说明：
 * 发 卡UI
 * @Author：黄盛强
 * @Date：2012-03-20
 */
public class MemberCreateCashCardPanel extends BaseDialog {
	
	private static final long serialVersionUID = 1L;
	private JLabelJTextFieldComponent number = null;//会员编号
	private JLabelJTextFieldComponent catalogID = null;///会员卡号
	private JLabelJTextFieldComponent nameTF = null; //姓名
	private JLabelJComboBoxComponent userType = null; //使用类型
	private JLabelJComboBoxComponent pulishType = null; //发放类型
	private DataPackageComponent startTimeTF = null;//开始时间
	private DataPackageComponent endTimeTF = null;//结束时间
	//private JLabelJTextFieldComponent sexTF = null; //性别
	private JLabelJTextFieldComponent needPoint = null; //手机号码
	private JLabelJTextFieldComponent initMoneyTF = null;//初始金额
	
	private JButton sureButton = null; //确定
	private JButton cancelButton = null; //取消
	public boolean isSuccess=false;//判断是否添加成功
	private TCashCardService tCashCardService=BuildSpringApplication.getInstance().getTCashCardServiceImpl();

	public MemberCreateCashCardPanel(JDialog jd, String title, boolean b) {
			
			super(jd,title,b);
			this.init();
			this.setSize(400, 550);
			this.setLocationRelativeTo(null);
			this.setResizable(false);
		}
	private void init(){
		this.addComponents();
		this.initListener();
	}
	private void addComponents() {
		JPanel main_jp=new JPanel();
		main_jp.setBorder(BorderFactory.createTitledBorder("创建优惠卷"));
		BoxLayout boxLayout=new BoxLayout(main_jp, BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		
		FlowLayout flowLayout=new FlowLayout(FlowLayout.CENTER);
		//JPanel cardNum_jp=new JPanel(flowLayout);
		catalogID = new JLabelJTextFieldComponent("  *", "卡    号：",30);
		//cardNum_jp.add(catalogID);
		number = new JLabelJTextFieldComponent("   *", "会员编号：",30);
		number.getText().setText(UUID.GenUUID15());
		number.getText().setEditable(false);
		nameTF = new JLabelJTextFieldComponent("   *", "会员名称：", 30);
		needPoint= new JLabelJTextFieldComponent("   *", "积分：", 30);
		//happenTimeTF=new DataPackageComponent("创建时间：",30);
		startTimeTF=new DataPackageComponent("开始时间：",30);
		endTimeTF=new DataPackageComponent("结束时间：",30);
		initMoneyTF = new JLabelJTextFieldComponent("   *", "初始金额：", 30);
		userType= new JLabelJComboBoxComponent("   *", "使用类型：",new String[]{},188);
		userType.addElement(new JComboBoxPOJO("请选择……"));
		userType.addElement(new JComboBoxPOJO("一次性优惠卷"));
		pulishType= new JLabelJComboBoxComponent("   *", "发放类型：",new String[]{},188);
		pulishType.addElement(new JComboBoxPOJO("请选择……"));
		pulishType.addElement(new JComboBoxPOJO("用户领取"));
		pulishType.addElement(new JComboBoxPOJO("店铺发送"));
		initMoneyTF.getText().setText("0.00");
		needPoint.getText().setText("0");
		JPanel jp6 = new JPanel(flowLayout);
		sureButton = new RPJButtonBlue("确定(Ctrl+Y)");
		cancelButton = new RPJButtonGray("取消");
		
		jp6.add(sureButton);
		jp6.add(cancelButton);
		jp6.setOpaque(false);
		main_jp.add(number);
		main_jp.add(catalogID);
		main_jp.add(nameTF);
		//main_jp.add(sexTF);
		//main_jp.add(happenTimeTF);
		main_jp.add(startTimeTF);
		main_jp.add(endTimeTF);
		main_jp.add(needPoint);
		main_jp.add(userType);
		main_jp.add(pulishType);
		main_jp.add(initMoneyTF);
		main_jp.add(jp6);
		this.add(main_jp);
	}
	
	private void initListener(){
		sureButton.addActionListener(this);
		cancelButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==sureButton){
			//提交
			String name=nameTF.getText().getText().trim();//会员姓名
			//int sex=Integer.valueOf(sexTF.getText().getText().trim());//会员姓名
			String cardNum=catalogID.getText().getText().trim();//
			String mobile=needPoint.getText().getText().trim(); //手机号码
			String initMoney=initMoneyTF.getText().getText().trim();//初始金额
			if(cardNum.equals("")||cardNum.length()>15){
				JOptionPane.showMessageDialog(null, "卡号不能为空，且不能大于15个字");
				catalogID.getText().requestFocus();
				return;
			}
			if(name.equals("")||name.length()>20){
				JOptionPane.showMessageDialog(null, "会员名称不能为空，且不能大于20个字");
				nameTF.getText().requestFocus();
				return;
			}
			if(startTimeTF.getText().equals("")){
				JOptionPane.showMessageDialog(null, "优惠卷开始时间不能为空！");
				return;
			}
			if(endTimeTF.getText().equals("")){
				JOptionPane.showMessageDialog(null, "优惠卷结束时间不能为空！");
				return;
			}
			if(userType.getBox().getSelectedIndex() == 0){
				JOptionPane.showMessageDialog(null, "请选择使用类型");
				return;
			}
			if(pulishType.getBox().getSelectedIndex() == 0){
				JOptionPane.showMessageDialog(null, "请选择发放类型");
				return;
			}
			if(initMoney.equals("")||!MatcherFormatUtil.MatcherPriceScheme(initMoney)){
				JOptionPane.showMessageDialog(null, "初始金额不能为空，请确认格式是否正确");
				initMoneyTF.getText().requestFocus();
				return;
			}
			Date date=new Date();
//			TSendCardRecord tSendCardRecord=new TSendCardRecord();
//			tSendCardRecord.setSendCardRecordID(UUID.GenUUID15());
//			tSendCardRecord.setCardNum(cardNum);
//			tSendCardRecord.setSendCardTime(date);
			
			TCashCard tCashCard=new TCashCard();
			tCashCard.setCardID(number.getText().getText().trim());
			tCashCard.setCreator(name);
			tCashCard.setMoney(Double.parseDouble(initMoney));
			tCashCard.setNeedPoint(Integer.parseInt(mobile));
			tCashCard.setEndTime(endTimeTF.getDate());
			tCashCard.setStartTime(startTimeTF.getDate());
			tCashCard.setHappenTime(date);
			tCashCard.setCatalogID(cardNum);
			tCashCard.setUseType(userType.getBox().getSelectedIndex()+"");
			tCashCard.setPublishType(pulishType.getBox().getSelectedIndex()+"");
			//tCashCard.getTMyCashCards().add(tSendCardRecord);
			try {
				
				tCashCardService.saveService(tCashCard);
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("会员发卡，卡号为：+"+catalogID.getText().getText().trim());
				JOptionPane.showMessageDialog(null, "操作成功");
				this.isSuccess=true;
				this.clear();
				this.dispose();
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
				this.clear();
			}
		}else if(e.getSource()==cancelButton){
			this.dispose();
		}
	}
	public void showDialog(){
		this.setVisible(true);
	}
	private void clear(){
		catalogID.getText().setText("");//会员卡号
		nameTF.getText().setText(""); //会员姓名
		needPoint.getText().setText("") ; //手机号码
		initMoneyTF .getText().setText("");//初始金额
		//sexTF.getText().setText("");
		catalogID.getText().requestFocus();
		
	}
}
